const app = getApp()
const _request = require("../../utils/request.js");
Page({
  data: {
    couponList: [1, 2, 3],//券列表
    cardinfo:'',//选中券
  },
  onLoad: function (options) {
    this.setData({
      cardinfo:JSON.parse(options.cardinfo)
    })
    this.loadData()//获取券列表
  },
  onUnload: function () {
    var that = this;
    var pages = getCurrentPages(); 
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      cardinfo: that.data.cardinfo
    })
  },
  loadData() {
    _request.get('/cashCoupon/getUserCashCouponByUserId', {}, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        res.data.data.map(item=>{
          item.checked = false
          if(this.data.cardinfo && this.data.cardinfo.id == item.id){
            item.checked = true
          }
        })
        this.setData({
          couponList:res.data.data
        })
      }
    })
  },
  onChange(e){
    var indexs = e.currentTarget.dataset.index
    var _list = this.data.couponList
    var _info = ''
    _list.map((item,index)=>{
      if(indexs == index){
        item.checked = true
        _info = item
      }else{
        item.checked = false
      }
    })
    this.setData({
      couponList:_list,
      cardinfo:_info,
    })
  },
})